<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发货计划号" prop="deliveryPlanNo">
        <el-input
          v-model="queryParams.deliveryPlanNo"
          placeholder="请输入发货单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货单号" prop="waybillNo">
        <el-input
          v-model="queryParams.waybillNo"
          placeholder="请输入发货单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectNo">
        <el-select v-model="queryParams.projectNo" filterable allow-create @keyup.enter.native="handleQuery" default-first-option placeholder="发货计划号">
          <el-option v-for="item in projectName1" :key="item.value" :label="item.projectNo" :value="item.projectNo">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="构件" prop="compWarehouseName">
        <el-input
          v-model="queryParams.compWarehouseName"
          placeholder="请输入构件名称或编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划状态" prop="deliveryPlanState">
        <el-select v-model="queryParams.deliveryPlanState"  placeholder="发货计划号">
          
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发货时间" >
        <el-date-picker clearable v-model="queryParams.beginDeliverGoodsTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择时间">
        </el-date-picker>
        - <el-date-picker clearable v-model="queryParams.endDeliverGoodsTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yard:DeliveryPlanInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yard:DeliveryPlanInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div style="float: right; ">
            构件总数量：<span style="color:red">
                {{count}}
            </span>
            构件总重量：
            <span style="color:red">
                {{weight}}
            </span>
        </div>
    <el-table v-loading="loading" :data="DeliveryPlanInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="发货计划号" align="center" prop="deliveryPlanNo" />
      <el-table-column label="状态" align="center" prop="deliveryPlanState" :formatter="deliveryPlanStateFormat"/>
      <el-table-column label="项目名称" align="center" prop="projectNo" />
      <el-table-column label="构件" align="center" prop="compWarehouseName" />
      <el-table-column label="发货单" align="center" prop="waybillInfo" />
      <el-table-column label="要货单位" align="center" prop="needCompany" />
      <el-table-column label="计划发货重量(t)" align="center" prop="planWeight" />
      <el-table-column label="计划发货数量" align="center" prop="planCount" />
      <el-table-column label="实际发货重量(t)" align="center" prop="actualWeight" />
      <el-table-column label="实际发货数量" align="center" prop="actualCount" />
      <el-table-column label="收货地址" align="center" prop="receivingAddress" />
      <el-table-column label="发货日期" align="center" prop="deliverGoodsTime" />
      <el-table-column label="审批人" align="center" prop="approverBy" />
      <el-table-column label="审批时间" align="center" prop="approverTime" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      
      <el-table-column label="备注" align="center" prop="comment" />
      <el-table-column label="发货时间" align="center" prop="deliverGoodsTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliverGoodsTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="lookOver(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yard:DeliveryPlanInfo:edit']"
          >修改</el-button>
          <!-- v-hasPermi="['yard:DeliveryPlanInfo:edit']" -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yard:DeliveryPlanInfo:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="byMeansOf(scope.row)"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="turnDown(scope.row)"
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    
  </div>
</template>

<script>
import { listDeliveryPlanInfo, getDeliveryPlanInfo, delDeliveryPlanInfo, addDeliveryPlanInfo, updateDeliveryPlanInfo ,getCompWarehouseStatistics,ToExamine} from "@/api/yard/DeliveryPlanInfo";

export default {
  name: "DeliveryPlanInfo",
  data() {
    return {
      count:0,
      weight:0,
      options: [
        {
          value: '0',
          label: '未送检'
        },
        {
          value: '1',
          label: '已送检'
        },
        {
          value: '2',
          label: '通过'
        },
        {
          value: '3',
          label: '被驳回'
        },
        {
          value: '4',
          label: '已完成'
        },
      ],
      // 计划状态下拉
      planStateList:[],
      // 项目编号下拉
      projectName1:[],
      // 发货计划编号下拉
      deliveryName:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDeliveryCompRef: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发货计划表格数据
      DeliveryPlanInfoList: [],
      // 发货计划_构件表格数据
      deliveryCompRefList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deliveryPlanNo: null,
        projectNo: null,
        deliveryPlanState: null,
        comment: null,
        deliverGoodsTime: null,
        waybillNo: null,
        compWarehouseName: null,
        needCompany: null,
        planWeight: null,
        actualWeight: null,
        planCount: null,
        actualCount: null,
        receivingAddress: null,
        approverBy: null,
        approverTime: null,
        createTime: null,
        beginDeliverGoodsTime: null,
        endDeliverGoodsTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    deliveryPlanStateFormat (row, column, cellValue, index) {

      if(cellValue==0){
        return "未送检";
      }else if(cellValue==1){
        return "已送检";
      }else if(cellValue==2){
        return "通过";
      }else if(cellValue==3){
         return "被驳回";
      }else if(cellValue==4){
        return "已完成";
      }else{
        return "";
      }
        
    },
    /** 查询发货计划列表 */
    getList() {
      this.loading = true;
      listDeliveryPlanInfo(this.queryParams).then(response => {
        this.DeliveryPlanInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
        for(let i = 0;i<response.rows.length;i++){
          this.deliveryName = response.rows[i].deliveryPlanNo
          this.deliveryName = response.rows
          this.projectName1 = response.rows[i].projectNo
          this.projectName1 = response.rows
          this.planStateList = response.rows[i].deliveryPlanState
          this.planStateList = response.rows

          if(this.DeliveryPlanInfoList[i].deliveryCompRefList==undefined){
            this.DeliveryPlanInfoList[i].compWarehouseName='共0件';
          }else{
            let s=this.DeliveryPlanInfoList[i].deliveryCompRefList[0];
            this.DeliveryPlanInfoList[i].compWarehouseName=s.compomentName+'共'+this.DeliveryPlanInfoList[i].deliveryCompRefList.length+'件';
          }
          if(this.DeliveryPlanInfoList[i].waybillInfoList==undefined){
            this.DeliveryPlanInfoList[i].waybillInfo='共0件';
          }else{
            let s=this.DeliveryPlanInfoList[i].waybillInfoList[0];
            this.DeliveryPlanInfoList[i].waybillInfo=s.waybillNo+'共'+this.DeliveryPlanInfoList[i].waybillInfoList.length+'件';
          }
        }
      });
     
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deliveryPlanNo: null,
        projectNo: null,
        deliveryPlanState: null,
        comment: null,
        deliverGoodsTime: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        waybillNo: null,
        compWarehouseName: null,
        needCompany: null,
        planWeight: null,
        actualWeight: null,
        planCount: null,
        actualCount: null,
        receivingAddress: null,
        approverBy: null,
        approverTime: null,
      };
      this.deliveryCompRefList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.open = true;
      // this.title = "添加发货计划";
      this.$router.push('/DeliveryPlanInfo/duichangXj')
    },
    // 通过
    byMeansOf(row){
      ToExamine({
      "id":row.id,
      "state":2
    }).then(res=>{
        console.log(res,12321321321321);
        this.$modal.msgSuccess("已通过！！！");
      })
    },
    // 驳回
    turnDown(row){
    
      ToExamine({
      "id":row.id,
      "state":3
    }).then(res=>{
        console.log(res,12321321321321);
        this.$modal.msgSuccess("已驳回！！！");
      })
    },
    /** 查看按钮操作 */
    lookOver(row) {
      this.$router.push({path:'/DeliveryPlanInfo/lian',query:{id:row.id}})
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDeliveryPlanInfo(id).then(response => {
        this.form = response.data;
        this.deliveryCompRefList = response.data.deliveryCompRefList;
        this.$router.push({path:'/DeliveryPlanInfo/duichangBj',query:{id:row.id}})
        this.title = "修改发货计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.deliveryCompRefList = this.deliveryCompRefList;
          if (this.form.id != null) {
            updateDeliveryPlanInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeliveryPlanInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除发货计划编号为"' + ids + '"的数据项？').then(function() {
        return delDeliveryPlanInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 发货计划_构件序号 */
    rowDeliveryCompRefIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 发货计划_构件添加按钮操作 */
    handleAddDeliveryCompRef() {
      let obj = {};
      obj.projectNo = "";
      obj.warehouseCompomentId = "";
      obj.needCount = "";
      this.deliveryCompRefList.push(obj);
    },
    /** 发货计划_构件删除按钮操作 */
    handleDeleteDeliveryCompRef() {
      if (this.checkedDeliveryCompRef.length == 0) {
        this.$modal.msgError("请先选择要删除的发货计划_构件数据");
      } else {
        const deliveryCompRefList = this.deliveryCompRefList;
        const checkedDeliveryCompRef = this.checkedDeliveryCompRef;
        this.deliveryCompRefList = deliveryCompRefList.filter(function(item) {
          return checkedDeliveryCompRef.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleDeliveryCompRefSelectionChange(selection) {
      this.checkedDeliveryCompRef = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yard/DeliveryPlanInfo/export', {
        ...this.queryParams
      }, `DeliveryPlanInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
